package com.escom.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.escom.entidad.EnfermedadVO;
import com.escom.entidad.HistoriaFamiliarVO;
import com.escom.entidad.ParentescoVO;
import com.escom.utils.Constantes;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Carlos on 7/04/14.
 */
public class HistoriaFamiliarDAO {

    private Context context;


    public HistoriaFamiliarDAO(Context context) {
        this.context = context;
    }

    public void createEnfermedad(EnfermedadVO[] enfermedades) {
        DataBaseManager dataBaseManager = new DataBaseManager(this.context);
        SQLiteDatabase sqLiteDatabase = dataBaseManager.getWritableDatabase();
        if (sqLiteDatabase != null) {

            for (int i = 0; i < enfermedades.length; i++) {
                EnfermedadVO enfermedad = enfermedades[i];
                ContentValues contentValues = new ContentValues();
                contentValues.put(DataBaseManager.ID_ENFERMEDAD, enfermedad.getIdEnfermedad());
                contentValues.put(DataBaseManager.ENFERMEDAD, enfermedad.getEnfermedad());
                try {
                    sqLiteDatabase.insertOrThrow(DataBaseManager.HLC_ENFERMEDAD_CAT, null, contentValues);
                } catch (android.database.sqlite.SQLiteConstraintException exSQL) {
                    Log.e(Constantes.LOG_ID, "Error administrado Insert");
                }
            }
            sqLiteDatabase.close();
        }
    }

    public void createParentesco(ParentescoVO[] parentescos) {
        DataBaseManager dataBaseManager = new DataBaseManager(this.context);
        SQLiteDatabase sqLiteDatabase = dataBaseManager.getWritableDatabase();
        if (sqLiteDatabase != null) {
            for (int i = 0; i < parentescos.length; i++) {
                ParentescoVO parentesco = parentescos[i];
                ContentValues contentValues = new ContentValues();
                contentValues.put(DataBaseManager.ID_PARENTESCO, parentesco.getIdParentesco());
                contentValues.put(DataBaseManager.TIPO_PARENTESCO, parentesco.getTipoParentesco());
                try {
                    sqLiteDatabase.insertOrThrow(DataBaseManager.HLC_PARENTESCO_CAT, null, contentValues);
                } catch (android.database.sqlite.SQLiteConstraintException exSQL) {
                    Log.e(Constantes.LOG_ID, "Error administrado Insert");
                }

            }
            sqLiteDatabase.close();
        }
    }

    public void createHistoriaFamiliar(HistoriaFamiliarVO[] historiasFamiliar) {
        DataBaseManager dataBaseManager = new DataBaseManager(this.context);
        SQLiteDatabase sqLiteDatabase = dataBaseManager.getWritableDatabase();
        if (sqLiteDatabase != null) {
            for (int i = 0; i < historiasFamiliar.length; i++) {
                HistoriaFamiliarVO historiaFamiliar = historiasFamiliar[i];
                ContentValues contentValues = new ContentValues();
                contentValues.put(DataBaseManager.ID_HISTORIA_FAMILIAR, historiaFamiliar.getIdHistoriaFamiliar());
                contentValues.put(DataBaseManager.ID_PACIENTE_MEDICO, historiaFamiliar.getIdPacienteMedico());
                contentValues.put(DataBaseManager.ID_ENFERMEDAD, historiaFamiliar.getEnfermedad().getIdEnfermedad());
                contentValues.put(DataBaseManager.ID_PARENTESCO, historiaFamiliar.getParentesco().getIdParentesco());
                try {
                    sqLiteDatabase.insertOrThrow(DataBaseManager.HLC_HISTORIA_FAMILIAR_TAB, null, contentValues);
                } catch (android.database.sqlite.SQLiteConstraintException exSQL) {
                    Log.e(Constantes.LOG_ID, "Error administrado Insert");
                }
            }
            sqLiteDatabase.close();
        }

    }


    public List<HistoriaFamiliarVO> retrieveHFamiliar() {
        Log.d("Logger HistoriaFamiliarDAO", "retrieveHFamiliar");
        DataBaseManager dataBaseManager = new DataBaseManager(this.context);
        SQLiteDatabase sqLiteDatabase = dataBaseManager.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT "
                + "hi.id_historia_Familiar,"
                + "hi.id_paciente_medico,"
                + "en.id_enfermedad,"
                + "par.id_parentesco,"
                + "en.enfermedad,"
                + "par.parentesco"
                + "FROM"
                + "hlc_historia_familiar_tab hi, hlc_parentesco_cat par , hlc_enfermedad_cat en"
                + "WHERE par.id_parentesco=hi.id_parentesco"
                + "AND en.id_enfermedad=hi.id_enfermedad", null);

        List<HistoriaFamiliarVO> his = new ArrayList<HistoriaFamiliarVO>();
        if (cursor.moveToFirst()) {
            do {
                HistoriaFamiliarVO historia = new HistoriaFamiliarVO();
                historia.setParentesco(new ParentescoVO());
                historia.setEnfermedad(new EnfermedadVO());
                historia.setIdHistoriaFamiliar(String.valueOf(cursor.getInt(0)));
                historia.setIdPacienteMedico(String.valueOf(cursor.getInt(1)));
                historia.getEnfermedad().setIdEnfermedad(String.valueOf(cursor.getInt(2)));
                historia.getEnfermedad().setEnfermedad(cursor.getString(4));
                historia.getParentesco().setIdParentesco(String.valueOf(cursor.getInt(3)));
                historia.getParentesco().setTipoParentesco(cursor.getString(5));
                Log.d("Logger HistoriaFamiliarDAO", historia.toString());
                his.add(historia);
            } while (cursor.moveToNext());
        } else {
            Log.d("Logger HistoriaFamiliarDAO", "NULO");
        }
        sqLiteDatabase.close();
        return his;
    }

    public List<HistoriaFamiliarVO> retrieveHistoraByIdPAciente(String idPaciente) {
        Log.d("Logger HistoriaFamiliarDAO", "retrieveHFamiliar");
        DataBaseManager dataBaseManager = new DataBaseManager(this.context);
        SQLiteDatabase sqLiteDatabase = dataBaseManager.getWritableDatabase();
        Log.d(Constantes.LOG_ID, this.getClass().getName() + " ID PAciente: " + idPaciente);

        Cursor cursor = sqLiteDatabase.rawQuery("SELECT "
                + "hi.id_historia_Familiar, "
                + "hi.id_paciente_medico, "
                + "en.id_enfermedad, "
                + "par.id_parentesco, "
                + "en.enfermedad,"
                + "par.tipo_parentesco "
                + "FROM "
                + "hlc_historia_familiar_tab hi, hlc_parentesco_cat par , hlc_enfermedad_cat en, "
                + "hlc_paciente_medico_tab pm "
                + "WHERE par.id_parentesco = hi.id_parentesco "
                + "AND en.id_enfermedad = hi.id_enfermedad "
                + "AND hi.id_paciente_medico = pm.id_paciente_medico "
                + "AND pm.id_paciente = ? ORDER BY par.id_parentesco ", new String[]{idPaciente});

       /* Cursor cursor = sqLiteDatabase.rawQuery("SELECT * "
                + "FROM "
                + "hlc_historia_familiar_tab hi ", null);*/


        Log.d(Constantes.LOG_ID, this.getClass().getName() + " Tamaño consulta: " + cursor.getCount());
        List<HistoriaFamiliarVO> his = new ArrayList<HistoriaFamiliarVO>();
        if (cursor.moveToFirst()) {
            do {
                /*Log.d(Constantes.LOG_ID, this.getClass().getName()
                        + " Columna 1: " + String.valueOf(cursor.getInt(0))
                        + " Columna 2: " + String.valueOf(cursor.getInt(1))
                        + " Columna 3: " + String.valueOf(cursor.getInt(2)));*/
                HistoriaFamiliarVO historia = new HistoriaFamiliarVO();
                historia.setParentesco(new ParentescoVO());
                historia.setEnfermedad(new EnfermedadVO());
                historia.setIdHistoriaFamiliar(String.valueOf(cursor.getInt(0)));
                historia.setIdPacienteMedico(String.valueOf(cursor.getInt(1)));
                historia.getEnfermedad().setIdEnfermedad(String.valueOf(cursor.getInt(2)));
                historia.getEnfermedad().setEnfermedad(cursor.getString(4));
                historia.getParentesco().setIdParentesco(String.valueOf(cursor.getInt(3)));
                historia.getParentesco().setTipoParentesco(cursor.getString(5));
                //Log.d("Logger HistoriaFamiliarDAO", historia.toString());
                his.add(historia);
            } while (cursor.moveToNext());
        } else {
            Log.d("Logger HistoriaFamiliarDAO", "NULO");
        }
        sqLiteDatabase.close();
        return his;
    }
}
